/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


package note.gui;

import note.vo.Note;
import note.dao.NoteDAO;
import javax.swing.JOptionPane;

/**
 *
 * @author tai
 */
public class MainFrame extends javax.swing.JFrame {

    /**
     * Creates new form MainFrame
     */
    public MainFrame() {
        initComponents();

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        addNewRecord = new javax.swing.JMenuItem();
        updateRecord = new javax.swing.JMenuItem();
        deleteRecord = new javax.swing.JMenuItem();
        showAllRecord = new javax.swing.JMenuItem();
        jMenuItem2 = new javax.swing.JMenuItem();
        jMenu3 = new javax.swing.JMenu();
        mnuManagerLog = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        mnupassword = new javax.swing.JMenuItem();
        jMenuItem3 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);

        jMenu1.setText("表的基本操作");

        jMenuItem1.setText("显示单条记录");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem1);

        addNewRecord.setText("添加新纪录");
        addNewRecord.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addNewRecordActionPerformed(evt);
            }
        });
        jMenu1.add(addNewRecord);

        updateRecord.setText("修改记录");
        updateRecord.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                updateRecordActionPerformed(evt);
            }
        });
        jMenu1.add(updateRecord);

        deleteRecord.setText("删除记录");
        deleteRecord.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteRecordActionPerformed(evt);
            }
        });
        jMenu1.add(deleteRecord);

        showAllRecord.setText("全部记录操作");
        showAllRecord.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                showAllRecordActionPerformed(evt);
            }
        });
        jMenu1.add(showAllRecord);

        jMenuItem2.setText("全部记录操作2");
        jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem2ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem2);

        jMenuBar1.add(jMenu1);

        jMenu3.setText("日志管理");

        mnuManagerLog.setText("管理日志");
        mnuManagerLog.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuManagerLogActionPerformed(evt);
            }
        });
        jMenu3.add(mnuManagerLog);

        jMenuBar1.add(jMenu3);

        jMenu2.setText("用户操作");

        mnupassword.setText("修改密码");
        mnupassword.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnupasswordActionPerformed(evt);
            }
        });
        jMenu2.add(mnupassword);

        jMenuItem3.setText("退出系统");
        jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem3ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem3);

        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 281, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void addNewRecordActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addNewRecordActionPerformed

        // TODO add your handling code here:
        new AddOneNoteFrame().setVisible(true);
    }//GEN-LAST:event_addNewRecordActionPerformed

    private void updateRecordActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_updateRecordActionPerformed
        String id = null;;
        while (true) {
            id = JOptionPane.showInputDialog(this, "请输入被修改记录的Id");
         // System.out.println(id);
            //注意id==null放在前面
            if (id == null) {
                return;
            }
            if (id.equals("")) {
                JOptionPane.showMessageDialog(this, "没有输入被修改记录的Id，请重新输入");
                continue;
            } else {
                try {
                    Integer.parseInt(id);
                } catch (NumberFormatException e) {
                    JOptionPane.showMessageDialog(this, "id请输入数字");
                    continue;
                }
                NoteDAO userDAO = new NoteDAO();
                Note note = userDAO.queryById(Integer.parseInt(id));

                if (note == null) {
                    JOptionPane.showMessageDialog(this, "输入的id不存在");
                    return;
                }
                new MofigyNoteJFrame(note).setVisible(true);
                break;
            }
        }

    }//GEN-LAST:event_updateRecordActionPerformed

    private void deleteRecordActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteRecordActionPerformed
        String id;

        while (true) {
            id = JOptionPane.showInputDialog(this, "请输入被删除记录的Id");
           //System.out.println(id);
            //注意id==null放在前面
            if (id == null) {
                return;
            }
            if (id.equals("")) {
                JOptionPane.showMessageDialog(this, "没有输入被删除记录的Id，请重新输入");
                continue;
            } else {
                try {
                    Integer.parseInt(id);
                } catch (NumberFormatException e) {
                    JOptionPane.showMessageDialog(this, "id请输入数字");
                    continue;
                }
            }
            break;
        }

        NoteDAO noteDAO = new NoteDAO();
        if (noteDAO.delete(Integer.parseInt(id))) {
            JOptionPane.showMessageDialog(this, "删除成功");
        } else {
            JOptionPane.showMessageDialog(this, "删除失败");
        }
    }//GEN-LAST:event_deleteRecordActionPerformed

    private void showAllRecordActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_showAllRecordActionPerformed
        // TODO add your handling code here:
        new AllNoteFrame().setVisible(true);
    }//GEN-LAST:event_showAllRecordActionPerformed

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        // TODO add your handling code here:
        ListNoteFrame listStudentFrame = new ListNoteFrame();
        listStudentFrame.setVisible(true);
        this.add(listStudentFrame);
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem3ActionPerformed
        // TODO add your handling code here:
        System.exit(0);
    }//GEN-LAST:event_jMenuItem3ActionPerformed

    private void mnupasswordActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnupasswordActionPerformed
        // TODO add your handling code here:
        new ModifyPasswordFrame().setVisible(true);
    }//GEN-LAST:event_mnupasswordActionPerformed

    private void mnuManagerLogActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuManagerLogActionPerformed
        // TODO add your handling code here:
        new LogInfoFrame().setVisible(true);
    }//GEN-LAST:event_mnuManagerLogActionPerformed

    private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed
        // TODO add your handling code here:
        new AllNoteFrame1().setVisible(true);
    }//GEN-LAST:event_jMenuItem2ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new MainFrame().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem addNewRecord;
    private javax.swing.JMenuItem deleteRecord;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JMenuItem mnuManagerLog;
    private javax.swing.JMenuItem mnupassword;
    private javax.swing.JMenuItem showAllRecord;
    private javax.swing.JMenuItem updateRecord;
    // End of variables declaration//GEN-END:variables

}
